package com.yazdankhah.tests;

import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import org.richfaces.component.UITree;
import org.richfaces.event.TreeSelectionChangeEvent;
import org.richfaces.model.TreeNode;
import org.richfaces.model.TreeNodeImpl;

/**
 *
 * @author ahmad
 */
@ManagedBean
@ViewScoped
public class RichFacesTreeNodeBean {

  private TreeNode rootNode = null;

  private List<String> selection = new ArrayList<String>();


  public List<String> getSelection() {
    return selection;
  }


  public void setSelection(List<String> selection) {
    this.selection = selection;
  }


  public void selectionChanged(TreeSelectionChangeEvent event) {
    selection.clear();
    UITree tree = (UITree) event.getComponent();
    Object storedRowKey = tree.getRowKey();
    
    for (Object rowKey : event.getNewSelection()) {
      tree.setRowKey(rowKey);
      selection.add(((RichFacesTreeNode) tree.getRowData()).getData());
    }
    tree.setRowKey(storedRowKey);
  }


  public void initNodes() {
    rootNode = new TreeNodeImpl();
    RichFacesTreeNode node = new RichFacesTreeNode("Desktop Type");
    node.addChild("1_1", new RichFacesTreeNode("Compact"));
    node.addChild("1_2", new RichFacesTreeNode("Everyday"));
    node.addChild("1_3", new RichFacesTreeNode("Gaming"));
    node.addChild("1_1_1", new RichFacesTreeNode("Premium"));
    rootNode.addChild("1", node);
// other initialization code
  }


  public void setRootNode(TreeNode rootNode) {
    this.rootNode = rootNode;
  }


  public TreeNode getRootNode() {
    if (rootNode == null) {
      initNodes();
    }
    return rootNode;
  }

}
